//
//  KAFindViewController.m
//  KeepAccount
//
//  Created by Veer on 2017/12/27.
//  Copyright © 2017年 Veer. All rights reserved.
//

#import "KAFindViewController.h"
#import "VRWebViewController.h"

#import "KAFindHeaderView.h"
#import "KAAccountModel.h"

@interface KAFindViewController ()

@property (nonatomic, weak) KAFindHeaderView *headerView;
@property (nonatomic, weak) UIScrollView *scrollView;
@property (nonatomic, weak) UIView *containerView;

@end

@implementation KAFindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"发现";
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self setupSubView];
    [self getAccountData];
    
    
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger thisYear = components.year;
    NSInteger thisMonth = [components month];
    
    self.headerView.month = thisMonth;
    
    // 注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleNotification:) name:kAddAccountNotificationName object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kAddAccountNotificationName object:nil];
}

- (void)setupSubView {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
    //
    
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.alwaysBounceVertical = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self.view);
        make.top.mas_equalTo(NAVIGATION_BAR_HEIGHT);
        make.bottom.mas_equalTo(0);
    }];
    
    UIView *containerView = [[UIView alloc] init];
    [scrollView addSubview:containerView];
    self.containerView = containerView;
    [containerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(scrollView);
        make.width.mas_equalTo(scrollView);
    }];
    
    KAFindHeaderView *headerView = [[KAFindHeaderView alloc] init];
    [containerView addSubview:headerView];
    self.headerView = headerView;
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(0);
        make.height.mas_equalTo(98);
    }];
    
    UIView *separatorView = [[UIView alloc] init];
    separatorView.backgroundColor = RGB(245, 245, 245);
    [containerView addSubview:separatorView];
    [separatorView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headerView.mas_bottom);
        make.left.right.mas_equalTo(containerView);
        make.height.mas_equalTo(10);
    }];
    
    [self setupBottomButton];
    
    
}

- (void)setupBottomButton
{
    
    UILabel *titleLbl = [[UILabel alloc] init];
    titleLbl.text = @"常用功能";
    titleLbl.font = VRFont(16);
    titleLbl.textColor = RGB(51, 51, 51);
    [self.containerView addSubview:titleLbl];
    [titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom).offset(10);
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(50);
    }];
    
    NSArray *titleArray = @[@"二手交易", @"二手车", @"宠物", @"家政"];
    NSArray *imgArray = @[@"category_i_finance_s", @"category_e_car_s", @"category_e_pet_s", @"category_e_repair_s"];
    CGFloat btnWH = kScreenWidth / 4;
    
    UIView *lastView = nil;
    for (int i = 0; i < 4; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.tag = i;
        btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
        [btn setImage:[UIImage imageNamed:imgArray[i]] forState:UIControlStateNormal];
        btn.imageEdgeInsets = UIEdgeInsetsMake(8, 0, 8, 0);
        [btn addTarget:self action:@selector(onBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.containerView addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(titleLbl.mas_bottom);
            make.left.mas_equalTo(btnWH * i);
            make.width.mas_equalTo(btnWH);
            make.height.mas_equalTo(60);
        }];
        
        UILabel *btnTitleLbl = [[UILabel alloc] init];
        btnTitleLbl.text = titleArray[i];
        btnTitleLbl.font = VRFont(15);
        btnTitleLbl.textColor = RGB(105, 105, 105);
        [self.containerView addSubview:btnTitleLbl];
        [btnTitleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(btn.mas_bottom).offset(4);
            make.centerX.mas_equalTo(btn);
        }];
        
        lastView = btnTitleLbl;
    }
    
    [self.containerView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(lastView);
    }];
}

#pragma mark - getdata
- (void)getAccountData
{
    
    NSDate *currentDate = [NSDate date];
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *components = [calendar components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay fromDate:currentDate];
    NSInteger thisYear = components.year;
    NSInteger thisMonth = [components month];
    NSString *yearStr = [NSString stringWithFormat:@"%ld", thisYear];
    NSString *monthStr = [NSString stringWithFormat:@"%ld", thisMonth];
    NSMutableArray *accountModelArray = [[KADataBase sharedDataBase] getAccountModelWithYear:yearStr month:monthStr];
    
    NSString *dateStr = nil;
    double totalIncome = 0;
    double totalExpenditure = 0;
    double balance = 0;
    for (KAAccountModel *accountModel in accountModelArray) {
        
        if (accountModel.type == 1) {
            totalIncome += accountModel.money;
        }else{
            totalExpenditure += accountModel.money;
        }
    }
    balance = totalIncome - totalExpenditure;
    
    // 设置topView的金额
    self.headerView.income = totalIncome;
    self.headerView.expenditure = totalExpenditure;
    self.headerView.balance = balance;
}

#pragma mark - action
- (void)onBtnClick:(UIButton *)sender
{
    NSArray *urlStrArray = @[@"http://m.58.com/hz/?reform=pcfront",
                             @"http://m.58.com/w/ershouche/",
                             @"http://m.58.com/hz/pets.shtml?58hm=m_home_pets_new&58cid=79&from=home_pets&PGTID=0d100000-0004-f5d0-51ba-e1704d7afab9&ClickID=2",
                             @"http://m.58.com/hz/shenghuo.shtml?58hm=m_home_shenghuo_new&58cid=79&from=home_shenghuo&PGTID=0d100000-0004-f587-d73d-95804f3785fc&ClickID=2"];
    NSArray *titleArray = @[@"二手交易", @"二手车", @"宠物", @"家政"];
    
    VRWebViewController *webVc = [[VRWebViewController alloc] init];
    webVc.isHaveNav = NO;
    webVc.urlStr = urlStrArray[sender.tag];
    webVc.title = titleArray[sender.tag];
    [self.navigationController pushViewController:webVc animated:YES];
}

- (void)handleNotification:(NSNotification *)notification
{
    [self getAccountData];
}


@end
